package com.zhongkai.service.impl;

import com.google.gson.Gson;
import com.mysql.jdbc.StringUtils;
import com.zhongkai.CryptoUtil;
import com.zhongkai.StringStatic;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.WeiXiuLoginService;
import com.zhongkai.jedis.JedisClient;
import com.zhongkai.model.utils.TTHelp;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Guard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class WeiXiuLoginServiceImpl implements WeiXiuLoginService {

    @Autowired
    private IBaseDao iBaseDao;

    @Autowired
    private JedisClient jedisClient;

    /**
     * 维修端登录
     *
     * @param phone 手机号
     * @param par   参数
     * @param types 登录方式 1密码 2验证码
     */
    @Override
    public ZhongkaiResult wxLogin(String phone, String par, String types) {
        if (StringUtils.isNullOrEmpty(phone) || StringUtils.isNullOrEmpty(types) || StringUtils.isNullOrEmpty(par)) {
            return ZhongkaiResult.build(400, "数据不完整,登录失败");
        }
        Guard guard;
        guard = (Guard) iBaseDao.get("from Guard gd where gd.guaPhone='" + phone + "'");

        if (guard == null) {
            return ZhongkaiResult.build(400, "用户不存在");
        }

        if ("3".equals(guard.getGuaState())) {
            return ZhongkaiResult.build(400, "您已离职,不可登录~");
        }
        if ("2".equals(guard.getGuaState())) {
            return ZhongkaiResult.build(400, "您在休假中,好好休息吧~");
        }


        if ("1".equals(types)) { //密码登录
            String pass = CryptoUtil.encode64(CryptoUtil.SALT, par);
            if (!pass.equals(guard.getGuaPass())) {

                return ZhongkaiResult.build(400, "密码错误,登录失败");
            }


        } else {

            try {
                String jedcode = jedisClient.get(phone);
                if (!par.equals(jedcode)) {
                    return ZhongkaiResult.build(400, "验证码错误");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ZhongkaiResult.build(500, "缓存服务器连接错误,请联系管理员");
            }


        }
/*        if (!StringUtils.isNullOrEmpty(guard.getGuaToken())){
            //   Long delete = jedisClient.delete(appUser.getToken());//生成新token之前删除及以前的
            try {
                jedisClient.expire(guard.getGuaToken(), 1);//过期时间为0
            } catch (Exception e) {
                e.printStackTrace();
                return ZhongkaiResult.build(500, "缓存服务器连接错误,请联系管理员");
            }

        }*/

      //  SendDate
        Long anomaly = (Long) iBaseDao.get("select count(su) from SendDate su where su.solve=2 and su.resolveUuid='"+guard.getGuaUuid()+"'");//异常设备数量 solve  1未完成 2 已经完成

        if (anomaly == 0 || anomaly == null) {
            guard.setGuaLevel(1);
        }else {
            guard.setGuaLevel(anomaly.intValue());

        }

        String generate = TTHelp.generate(); //生成Token
        String concat = StringStatic.TOKEN_WX.concat(generate);
        guard.setGuaToken(concat);

        String json = new Gson().toJson(guard);
        try {
            jedisClient.setkeyexp(concat, json, 604800);  //加入缓存
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(500, "缓存服务器出错,请联系管理员");
        }

        iBaseDao.update(guard);
        return ZhongkaiResult.ok(guard);

    }
}
